package zhengfuzhe.com.newupload1.other;

import android.content.Context;
import android.content.pm.PackageInfo;

import java.util.ArrayList;
import java.util.List;

import zhengfuzhe.com.newupload1.bean.LocalAppInfoBean;


public class LocalAppManager {

    private static LocalAppManager instance = null;
    private Context context;

    private LocalAppManager(Context context) {
        this.context = context;
    }

    public static LocalAppManager getInstance(Context context) {
        if (instance == null) {
            synchronized (LocalAppManager.class) {
                if (instance == null) {
                    instance = new LocalAppManager(context);
                }
            }
        }
        return instance;
    }

    /**
     * 获取所有的应用，除却系统的
     *
     * @return ArrayList<LocalAppInfoBean>（返回第三方应用的集合）
     */
    public ArrayList<LocalAppInfoBean> getAllApp() {
        ArrayList<LocalAppInfoBean> infoList = new ArrayList<LocalAppInfoBean>();
        List<PackageInfo> packageInfos = context.getPackageManager().getInstalledPackages(0);//获取所有的应用包名
        for (PackageInfo info : packageInfos) {//遍历所有应用
            LocalAppInfoBean appInfoBean = new LocalAppInfoBean();
            appInfoBean.appName = info.applicationInfo.loadLabel(context.getPackageManager()).toString();
            appInfoBean.appIcon = info.applicationInfo.loadIcon(context.getPackageManager());
            appInfoBean.packageName = info.packageName;
            appInfoBean.versionCode = info.versionCode;
            appInfoBean.versionName = info.versionName;
//            if ((info.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {//非系统应用
            infoList.add(appInfoBean);
//            }
        }
        return infoList;
    }
}
